package com.chilli.cosima.action.usermanager;

import java.util.List;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.common.BussinessException;
import com.chilli.cosima.data.bean.TPower;
import com.chilli.cosima.data.bean.TPowerRole;
import com.chilli.cosima.data.bean.TRole;
import com.chilli.cosima.service.UserService;

public class RoleEditAction extends BaseSupport
{

	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = -6764913130277169807L;

	private UserService userService;

	private Long id;

	private TRole role;

	String[] testCheck;

	public String jumpPage()
	{

		TRole role = userService.findRoleById(id);

		if (null != role)
		{
			try
			{
				this.role = role;

				// 获得所有权限
				List<TPower> powerlist = userService.findAllPowers();

				// 获得用户对应角色的权限
				List<TPowerRole> optpowerlist = userService
						.findRolePowerByRoleId(this.role.getId());

				// 创建用户权限树
				userService.clearUpdatePowerTreeList();
				userService.updateTree("0", optpowerlist, powerlist);

				// 获得权限树
				List<String> updatePowerTreeList = userService
						.getUpdatePowerTreeList();
				super.request.setAttribute("updatePowerTreeList",
						updatePowerTreeList);
				return JUMP_PAGE;
			} catch (Exception e)
			{
				logger.error("error", e);
			}

			return ERROR;
		} else
		{
			logger.error("error");
			return ERROR;
		}

	}

	public String execute()
	{
		try
		{
			// 修改角色
			userService.updateRole(role);

			// 删除表中之前的权限
			userService.deleteRolePower(new Long[]
			{ role.getId() });

			// 保存对应权限
			for (int i = 0; i < testCheck.length; i++)
			{
				TPowerRole powerRole = new TPowerRole();

				powerRole.setRoleid(role.getId());
				powerRole.setPowerid(testCheck[i]);

				// 保存权限
				userService.createPowerRole(powerRole);

			}

			return SUCCESS;
		} catch (BussinessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return JUMP_PAGE;
	}

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public TRole getRole()
	{
		return role;
	}

	public void setRole(TRole role)
	{
		this.role = role;
	}

	public String[] getTestCheck()
	{
		return testCheck;
	}

	public void setTestCheck(String[] testCheck)
	{
		this.testCheck = testCheck;
	}

}
